//
//  ClassificationMusicViewController.m
//  WXMusic
//
//  Created by 漫步人生路 on 15/8/31.
//  Copyright (c) 2015年 漫步人生路. All rights reserved.
//

#import "ClassificationMusicViewController.h"
#import "ClassCollectionViewCell.h"
#import "ClassCollectionReusableView.h"
#import "SingerCollectionViewCell.h"
#import "SingerCollectionReusableView.h"
#import "SingerModel.h"
#import "AllClassModel.h"
#import "MoreSingerTableViewController.h"
@interface ClassificationMusicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *classCollectionView;
}
@property (nonatomic, strong) NSMutableArray *singerArray;
@property (nonatomic, strong) NSMutableArray *allClassArray;
@property (nonatomic, strong) UILabel *label;
@end

@implementation ClassificationMusicViewController
- (NSMutableArray *)singerArray
{
    if (!_singerArray) {
        _singerArray = [[NSMutableArray alloc]init];
    }
    return _singerArray;
}
- (NSMutableArray *)allClassArray
{
    if (!_allClassArray) {
        _allClassArray = [[NSMutableArray alloc]init];
    }
    return _allClassArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:kBlurImageView];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    classCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 44, kWidth, kHeight - 100) collectionViewLayout:layout];
    classCollectionView.backgroundColor = [UIColor clearColor];
    classCollectionView.delegate = self;
    classCollectionView.dataSource = self;
    [self reloadDataForSinger];
    [self registerCellAndHeaderView];
    [self.view addSubview:classCollectionView];
}

- (void)setUpWithLabel:(UILabel *)label frame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor
{
    label = [[UILabel alloc]init];
    label.text = text;
    label.textColor = textColor;
    
}
//解析歌手
- (void)reloadDataForSinger
{
    [LORequestManger GET:kHotSingerUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *artist in dic[@"artist"]) {
            SingerModel *singerModel = [SingerModel shareJsonWithDictionary:artist];
            [self.singerArray addObject:singerModel];
        }
        [self.allClassArray addObject:self.singerArray];
        [self reloadAllClass];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)reloadAllClass
{
    for (int i = 0; i < 7; i++) {
        NSMutableArray *classArray = [[NSMutableArray alloc]init];
        if (i != 2) {
            [LORequestManger GET:[NSString stringWithFormat:kClassUrl,i] success:^(id response) {
                NSDictionary *dic = (NSDictionary *)response;
                for (NSDictionary *result in dic[@"result"]) {
                    AllClassModel *allClassModel = [AllClassModel shareJsonWithDictionary:result];
                    [classArray addObject:allClassModel];

                }
                [classCollectionView reloadData];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                
            }];
            [self.allClassArray addObject:classArray];
            NSLog(@"%ld",self.allClassArray.count);
        }

    }

}
- (void)registerCellAndHeaderView
{
    [classCollectionView registerClass:[ClassCollectionViewCell class] forCellWithReuseIdentifier:@"classItem"];
    [classCollectionView registerClass:[ClassCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"classHeader"];
    [classCollectionView registerClass:[SingerCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"singerView"];
    [classCollectionView registerClass:[SingerCollectionViewCell class] forCellWithReuseIdentifier:@"singerCell"];
}
//表头（增广视图）
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    NSArray *titleArray = @[@"活动",@"主题",@"心情",@"语言",@"年代",@"曲风"];
    if (indexPath.section == 0) {
        SingerCollectionReusableView *singerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"singerView" forIndexPath:indexPath];
        [singerView.moreSinger addTarget:self action:@selector(moreSingerView) forControlEvents:UIControlEventTouchUpInside];
        return singerView;
    }
    ClassCollectionReusableView *classReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"classHeader" forIndexPath:indexPath];
    classReusableView.title.text = [titleArray objectAtIndex:indexPath.section - 1];
    return classReusableView;
    
}
//增广视图size设置
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kWidth - 30, 45);
}
//增广视图button点击事件
- (void)moreSingerView
{
    MoreSingerTableViewController *moreSingerVC = [[MoreSingerTableViewController alloc]initWithStyle:UITableViewStylePlain];
    [self.navigationController pushViewController:moreSingerVC animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[self.allClassArray objectAtIndex:section] count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.allClassArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(kWidth / (375 / 80), kWidth / (375 / 100));
    }
    return CGSizeMake(kWidth / (375 / 90), kWidth / (375 / 90));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 30, 0, 30);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        SingerCollectionViewCell *singerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"singerCell" forIndexPath:indexPath];
        SingerModel *singerModel = [[self.allClassArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        [singerCell setValueWithModel:singerModel];
        return singerCell;
    }
    ClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"classItem" forIndexPath:indexPath];
    AllClassModel *allClassModel = [[self.allClassArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [cell setValueWithModel:allClassModel];
    return cell;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
